package com.ego.item.service.impl;

import com.ego.commons.utils.JsonUtils;
import com.ego.dubbo.service.TBItemCatDubboService;
import com.ego.dubbo.service.TBItemDubboService;
import com.ego.dubbo.service.TbItemDescDubboService;
import com.ego.dubbo.service.TbItemParamItemDubboService;
import com.ego.item.pojo.CategoryNode;
import com.ego.item.pojo.ItemCategoryNav;
import com.ego.item.pojo.ParamData;
import com.ego.item.pojo.ParamDataKAndV;
import com.ego.pojo.*;
import com.ego.item.service.ItemCatService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
@CacheConfig(cacheNames = "com.ego.item")
public class ItemCatServiceImpl implements ItemCatService {


    @DubboReference
    private TBItemCatDubboService tbItemCatDubboService;


    @DubboReference
    private TBItemDubboService tbItemDubboService;

    @DubboReference
    private TbItemDescDubboService tbItemDescDubboService;

    @DubboReference
    private TbItemParamItemDubboService tbItemParamItemDubboService;


    @Cacheable(key = "'selectAllCat'")
    public ItemCategoryNav selectAllCat() {

        ItemCategoryNav itemCategoryNav = new ItemCategoryNav();

        itemCategoryNav.setData(getItemCatByParentId(0l));

        return itemCategoryNav;
    }

    @Cacheable(key = "'itemDetail::'+#id")
    public TbItemDetail selectById(Long id) {

        TbItem tbItem = tbItemDubboService.selectById(id);


        TbItemDetail tbItemDetail = new TbItemDetail();

        //将TbItem对象数据copy到TbItemDetail
        BeanUtils.copyProperties(tbItem,tbItemDetail);



        tbItemDetail.setImages(StringUtils.isNoneBlank(tbItem.getImage())?tbItem.getImage().split(","):new String[1]);


        return tbItemDetail;
    }

    /**
     * 根据商品类目父id查询出对应的子类目数据
     * @param parentId 父id
     * @return 类目集合
     */
    public List<Object> getItemCatByParentId(long parentId){

        //消费Dubbo提供者的根据父id查询主对应的子类目的方法
        List<TbItemCat> tbItemCats = tbItemCatDubboService.selectItemCatsByParentId(parentId);

        List<Object> categoryNodes = new ArrayList<>();
        //循环集合，将每一个对象的数据重新获取出来封装成一个个CategoryNode对象中去
        for (TbItemCat tbItemCat : tbItemCats) {

            //只有当前数据是父id时候才有子数据，才需要递归查询
           if (tbItemCat.getIsParent()){
               CategoryNode categoryNode = new CategoryNode();
               categoryNode.setU("/products/"+tbItemCat.getId()+".html");
               categoryNode.setN("<a href='/products/"+tbItemCat.getId()+".html'>"+tbItemCat.getName()+"</a>");

               categoryNode.setI(getItemCatByParentId(tbItemCat.getId()));
               //将封装CategoryNode 添加到集合中
               categoryNodes.add(categoryNode);
           }else{
               categoryNodes.add("/products/"+tbItemCat.getId()+".html|"+tbItemCat.getName());
           }
        }

        return categoryNodes;

    }



    @Override
    public String selectItemDescByItemId(Long id) {

        TbItemDesc tbItemDesc = tbItemDescDubboService.selectByItemId(id);

        return tbItemDesc.getItemDesc();
    }

    @Override
    public String selectItemParamByItemId(Long id) {

        TbItemParamItem tbItemParamItem = tbItemParamItemDubboService.selectByItemId(id);



        String jsonStr = tbItemParamItem.getParamData();
        //[{"group":"主体","params":[{"k":"品牌","v":""},{"k":"型号","v":""},{"k":"颜色","v":""},{"k":"上市年份","v":""}]},{"group":"网络","params":[{"k":"4G网络制式","v":""},{"k":"3G网络制式","v":""},{"k":"2G网络制式","v":""}]},{"group":"存储","params":[{"k":"机身内存","v":""},{"k":"储存卡类型","v":""}]}]

        List<ParamData> paramData = JsonUtils.jsonToList(jsonStr, ParamData.class);

        StringBuilder sb = new StringBuilder();
        for (ParamData pd : paramData) {
            System.out.println("paramDatum = " + pd);

            sb.append("<table  style='width:100%'>");

            for (int i = 0; i <pd.getParams().size() ; i++) {

                ParamDataKAndV param = pd.getParams().get(i);

                sb.append("<tr>");
                //如果是第一行显示组名,其他行就空起来
                if(i==0){
                    sb.append("<td style='width:100px;text-align:right;padding-right:30px'>"+pd.getGroup()+"</td>");
                }else{
                    sb.append("<td></td>");
                }

                sb.append("<td style='width:100px;text-align:right;padding-right:30px'>"+param.getK()+"</td>");
                sb.append("<td>"+param.getV()+"</td>");

                   sb.append("</tr>");

            }


            sb.append("</table>");

            sb.append("<hr style='color:gray'>");
        }



        return sb.toString();
    }

    public void hi(HttpSession httpSession){
        httpSession.setAttribute("user","登录用户对象User");
        httpSession.setMaxInactiveInterval(10);

    }


}
